"use client";
import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";
import { Space, Tag, type TableProps } from 'antd';
import { Table, theme, Button, Drawer, Form, Input, Select, Checkbox } from "antd";
import AvaForm from "./AvaForm";
import Layout from "@/components/Layout";
import styles from "./index.module.less";
// 数据类型
interface DataType {
  key: string;
  name: string;
  role: number;
  desc: string;
  tags: string[];
}
export default function User() {
  const t = useTranslations("user");
  const { token } = theme.useToken();
  const [title,setTitle]=useState('')
  const [inp1,setInp1]=useState<any>(null)
  const [inp2,setInp2]=useState<any>(null)
  const [datas,setDatas]=useState({})
  const [form] = Form.useForm();
  const [open, setOpen] = useState(false);
  const showDrawer = () => {setOpen(true)};
  const onClose = () => {
    setOpen(false)
    form.resetFields()
  };
  const [searchData,setSearchData]=useState<any>()
  const [data, setData] = useState<DataType[]>([
    {
      key: '1',
      name: '徐小夕',
      role: 1,
      desc: 'H5-Dooring作者, 掘金签约作者，知乎专栏作家',
      tags: ['前端工程师', 'developer'],
    },
    {
        key: '2',
        name: '张三',
        role: 2,
        desc: '知乎专栏作家',
        tags: ['前端工程师', 'developer' , 'Dooring'],
    },
    {
        key: '3',
        name: '李盟',
        role: 2,
        desc: 'Dooring共建者',
        tags: ['后端工程师', 'V6.Dooring' , 'Dooring'],
    },
    {
        key: '4',
        name: '王阿明',
        role: 2,
        desc: '技术合伙人',
        tags: ['全栈工程师', '橙子表单'],
    },
    {
        key: '5',
        name: '张小明',
        role: 2,
        desc: '技术合伙人',
        tags: ['全栈工程师', '橙子表单'],
    },
    {
        key: '6',
        name: 'Tom',
        role: 2,
        desc: '技术合伙人',
        tags: ['全栈工程师', 'Next-Admin'],
    },
    {
        key: '7',
        name: 'Json',
        role: 2,
        desc: '技术合伙人',
        tags: ['全栈工程师', 'Next-Admin'],
    },
  ]) // 初始数据
  // 表格列配置
  const columns: TableProps<DataType>['columns'] = [
    {
      title: '用户名',
      dataIndex: 'name',
      key: 'name',
      fixed: 'left',
      width: 100
    },
    {
      title: '角色',
      dataIndex: 'role',
      key: 'role',
      render: role => role === 1 ? '超级管理员' : '开发者'
    },
    {
      title: '简介',
      dataIndex: 'desc',
      key: 'desc',
    },
    {
      title: '标签',
      key: 'tags',
      dataIndex: 'tags',
      render: (_, { tags }) => (
        <>
          {tags.map((tag) => {
            let color = tag.length > 5 ? 'geekblue' : 'green';
            if (tag === 'loser') {
              color = 'volcano';
            }
            return (
              <Tag color={color} key={tag}>
                {tag.toUpperCase()}
              </Tag>
            );
          })}
        </>
      ),
    },
    {
      title: '操作',
      key: 'action',
      render: (_, record) => (
        <Space size="middle">
          <a onClick={()=>{
            form.setFieldsValue(record)
            setTitle('编辑')
            setDatas(record)
            showDrawer()
          }}>编辑</a>
          <a onClick={()=>{
            let obj=data.filter((item:DataType) => item.key !== record.key)
            setSearchData(obj)
            setData(obj)
          }}>删除</a>
        </Space>
      ),
    },
  ];
  const listStyle: React.CSSProperties = {
    background: token.colorFillAlter,
    borderRadius: token.borderRadiusLG,
    padding: 12,
  };
  useEffect(() => {
    setSearchData(data)
  },[])
  return (
    <Layout curActive="/zcy">
      <main className={styles.userWrap}>
        <div className={styles.content}>
          <Space>
            <>名称：<Input type="text" value={inp1} onChange={(v)=>{setInp1(v.target.value)}} /></>
            
            <>职位：<Select
              value={inp2}
              style={{width: 120}}
              onChange={(v) => {setInp2(v)}}
              options={[{value: 1,label: '超级管理员',},{value: 2, label: '开发者', } ]} /></>
            <Button onClick={()=>{
              let arr=data.filter((item:DataType) => {
                if(inp1!==null && inp2==null){
                  return item.name.includes(inp1)?item:''
                }else if(inp1==null && inp2!==null){
                  return item.role==inp2?item:''
                }else{
                  return item.name.includes(inp1) && item.role.toString().includes(inp2)?item:''
                }
              })
              setSearchData(arr)
            }}>搜索</Button>
            <Button onClick={()=>{
              setInp1(null)
              setInp2(null)
              setSearchData(data)
            }}>重置</Button>
            <Button onClick={()=>{
              setTitle('新增')
              setOpen(true)
            }}>新增</Button>
          </Space>
          <div style={listStyle}>
            <h3>{t("zcy")}</h3>
            <Table
              columns={columns}
              dataSource={searchData}
              pagination={{ pageSize: 5 }}
              scroll={{ x: 1000 }}
            />
          </div>
        </div>
      </main>
      <Drawer title="Basic Drawer" onClose={onClose} open={open}>
        {
          open?<Form
            form={form}
            labelCol={{span: 8}}
            wrapperCol={{span: 16}}
            style={{maxWidth: 600}}
            initialValues={{remember: true}}
            onFinish={(v)=>{
              onClose()
              if(title==='新增'){
                v.key=data[data.length-1].key+1
                setData([...data,v]) 
              }else{
                let index=data.findIndex((item:DataType) => item.key == datas.key)
                console.log(index);
                let arr=[...data]
                arr[index]=v
                arr[index].key=datas.key
                setData(arr)
              }
              console.log(v);
            }}
            autoComplete="off"
          >
            <Form.Item
              label="name"
              name="name"
            >
              <Input />
            </Form.Item>

            <Form.Item
              label="role"
              name="role"
            >
              <Select>
                <Select.Option value={1}>超级管理员</Select.Option>
                <Select.Option value={2}>开发者</Select.Option>
              </Select>
            </Form.Item>
        
            <Form.Item
              label="desc"
              name="desc"
            >
              <Input />
            </Form.Item>

            <Form.Item label="tags" name="tags">
              <Checkbox.Group>
                <Checkbox key={11} value="前端工程师">前端工程师</Checkbox>
                <Checkbox key={12} value="后端工程师">后端工程师</Checkbox>
                <Checkbox key={13} value='developer'>developer</Checkbox>
                <Checkbox key={14} value="Dooring">Dooring</Checkbox>
                <Checkbox key={15} value="V6.Dooring">V6.Dooring</Checkbox>
                <Checkbox key={16} value="全栈工程师">全栈工程师</Checkbox>
                <Checkbox key={17} value="橙子表单">橙子表单</Checkbox>
                <Checkbox key={18} value="Next-Admin">Next-Admin</Checkbox>
              </Checkbox.Group>
            </Form.Item>
        
            <Form.Item label={null}>
              <Button type="primary" htmlType="submit">
                Submit
              </Button>
            </Form.Item>
          </Form>:''
        }
      </Drawer>
    </Layout>
  );
}
